package controller;

import gui.PackageShape;
import gui.Shape;

import java.io.File;
import java.util.ArrayList;

import model.OOModel;
import model.OOPackage;


public class PackageDiagramManager {

	private static ArrayList<Shape> packages = new ArrayList<Shape>();
	private static int currX = 163,currY = 10;
	
	private OOModel ooModel;

	// constructor takes the OOModel object
	public PackageDiagramManager(OOModel ooModel)
	{
		this.ooModel=  ooModel;
	}

	// JUST FOR TESTING
	@SuppressWarnings("unused")
	public static void main(String[] args)  {
		File dir = new File("D:\\UML Modeler");
		OOModel ooModel = new OOModel();
		
		// This test seems weird actually :D ... mla3bek
		
		DirectoryTraverseTree dtt = new DirectoryTraverseTree(null);
		dtt.processDirectory(dir);
		Tree treeOfPackages = dtt.getPtree();
		ArrayList<OOPackage> arr = new ArrayList<OOPackage>();
		arr.add(treeOfPackages.getRootElement());
		managePackages(arr);
	}
	
	
	/*
	 * arrOOPackages
	 *        |-> aParentPackage-> children .......
	 *        |-> anotherParentPackage -> children ........
	 *
	 */
	
	
	// we interact with the OOModel, take the OOPackage array from it
	public static void managePackages(ArrayList<OOPackage> arrOOPackages) {
		// loop on all "Parent" packages available
		for(OOPackage ooPackage: arrOOPackages)
		{
			managePackageSize(ooPackage);
			managePackageLocation(packages.get(packages.size()-1));
		}

	}

	private static void managePackageSize(OOPackage currentPackage)
	{
		PackageShape shape = new PackageShape();

		for (OOPackage child : currentPackage.getChildren() )
		{
			managePackageSize(child);
			shape.addChild((PackageShape)packages.get(packages.size()-1));
		}

		packages.add(shape);
		shape.setName(currentPackage.name);

		///
		int height = 60 * shape.getChildren().size()+ 60 ; 
		int width = currentPackage.name.length() * 5 + 16; //gc.stringExtent(currentPackage.name).x + 16 ; // package name length 

		if (currentPackage.getChildren().size()!=0)
		{	
			int maxWidth = shape.getChildren().get(0).getWidth();
			for(PackageShape child : shape.getChildren())
			{
				height = height + child.getHeight();
				if(maxWidth < child.getWidth())
					maxWidth = child.getWidth(); 
			}

			if(maxWidth >= (3*width)) width = maxWidth + 120 ; 
			else width = (width*3) + 120 ;
		}

		shape.setHeightWidth(height, width) ;	
		///
	}

	private static void managePackageLocation(Shape shp)
	{
		PackageShape currentPackage = (PackageShape)shp;
		currentPackage.setPoint(currX, currY);

		for (PackageShape child : currentPackage.getChildren())
		{
			currX = currX + 60 ;
			currY = currY + 60;
			managePackageLocation(child);
			currX = currX - 60 ;
			currY = currY + 60 ;			
		}		
	}

	// use OOModel to take the array list of packages inside it and generate shapes
	public void generateShapes()
	{
		managePackages(ooModel.packages);
		ControllerManager.getDefault().drawShapes(packages);
	}
}


